const fs = require("fs");
const path = require("path");
const express = require("express");
const auth = require("../middleware/auth");

const basename = path.basename(__filename);

const router = express.Router();

// 路由批量使用
fs.readdirSync(__dirname)
  .filter(
    (file) =>
      file.indexOf(".") !== 0 && file !== basename && file.slice(-3) === ".js"
  )
  .forEach((file) =>
    router.use(
      `/api/${file.replace(/\.js$/, "")}`,
      auth,
      require(path.join(__dirname, file))
    )
  );

module.exports = router;
